package com.ruoyi.zyt.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.RandomUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zyt.domain.User;
import com.ruoyi.zyt.service.IUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考生信息Controller
 * 
 * @author zyt
 * @date 2023-11-03
 */
@RestController
@RequestMapping("/zyt/user")
public class UserController extends BaseController
{
    @Autowired
    private IUserService userService;

    /**
     * 查询考生信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(User user)
    {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 导出考生信息列表
     */
    @Log(title = "考生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, User user)
    {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.exportExcel(response, list, "考生信息数据");
    }

    /**
     * 获取考生信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userService.selectUserById(id));
    }

    /**
     * 新增考生信息
     */
    @Log(title = "考生信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user)
    {
        User user1 = new User();
        user1.setName(user.getName());
        List<User> users = userService.selectUserList(user1);
        if(!users.isEmpty()){
            return error("用户名已存在！");
        }
        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改考生信息
     */
    @Log(title = "考生信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user)
    {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除考生信息
     */
    @Log(title = "考生信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userService.deleteUserByIds(ids));
    }


    /**
     * 考生登录
     * @param user
     * @return
     */
    @Log(title = "考生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/login")
    public AjaxResult login(@RequestBody User user)
    {
        List<User> list = userService.selectUserList(user);
        if(list.isEmpty()){
            long id = RandomUtils.nextLong(1L, 100000L);
            user.setId(id);
            userService.insertUser(user);
            list.add(user);
        }
        return success(list);
    }

    @Log(title = "考生信息", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult updateUser(@RequestBody User user)
    {
        return toAjax(userService.updateUser(user));
    }
}
